package animator.phantom.exec;

/*
    Copyright Janne Liljeblad 2006,2007,2008

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.util.*;
import java.io.*;

public class StreamBufferReader extends Thread
{
	private InputStream is;
	private int lines = 0;
	private StringBuffer buf = new StringBuffer();
	private String error = null;
	
	public StreamBufferReader( InputStream is )
	{
		this.is = is;
	}
	
	public void run()
	{
		try
		{
			InputStreamReader isr = new InputStreamReader( is );
			BufferedReader br = new BufferedReader( isr );
			String line = null;
			while ( ( line = br.readLine()) != null )
			{
				lines++;
				buf.append( line );
			}
			is.close();
			isr.close();
			br.close();
		} 
		catch ( IOException e )
		{
			error = new String( e.getMessage() );
		}
	}
	
	//--- Interface
	public String getBufferedString(){ return buf.toString(); }
	public int linesRead(){ return lines; }
	public boolean hadError(){ return ( error != null ); }

}//end class

